<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>分页</title>
    <style>
        div {
            width: 800px;
            height: 40px;
            border: 1px solid black;
            margin: 30px auto;
        }
    </style>
</head>

<body>
    <table border="1" cellspacing="0" style="margin: auto;">
        <tdead>
            <tr>
                <td>序号</td>
                <td>学生ID</td>
                <td>学生姓名</td>
                <td>学生年龄</td>
                <td>学生性别</td>
                <td>学生班级</td>
                <td>学生成绩</td>
            </tr>
        </tdead>
        <tbody class="tbody"></tbody>
    </table>
    <div class="page1"></div>
</body>
<script>
    // 用来准备数据
    const firstName = '赵钱孙李周吴郑王冯陈楚魏将沈韩杨朱琴尤许何吕施章'
    const laseName = '氢氦锂铍硼碳氮氧氟氖钠镁铝硅磷硫氯氩钾钙'
    const tdirdName = '一二三四五六七八九十'
    const gender = '男女'
    const fn = (a, b) => Math.floor(Math.random() * (b - a + 1)) + a

    const tableDataArr = []
    for (let i = 0; i <= 1000; i++) {
        tableDataArr.push({
            id: i + 1,
            name: firstName[fn(0, 23)] + laseName[fn(0, 19)] + tdirdName[fn(0, 9)],
            age: fn(16, 30),
            gender: gender[fn(0, 1)],
            class: fn(1910, 1920),
            score: fn(40, 100)
        })
    }
    //console.log(tableDataArr)
    //需求：分别显示1、2、3页数据
    //当前页码
    let pagenum = 1
    //每页显示数据条数
    let pagesize = 10
    //起始位置
    let startnum = (pagenum - 1) * pagesize
    //for (let i = 0; i < 10; i++) {
    for (let i = startnum; i < startnum + pagesize; i++) {
        //创建一个tr标签
        let trObj = document.createElement('tr')
        //向tr标签中放td数据
        trObj.innerHTML = `
        <td>${i + 1}</td>
        <td>${tableDataArr[i].id}</td>
        <td>${tableDataArr[i].name}</td>
        <td>${tableDataArr[i].age}</td>
        <td>${tableDataArr[i].gender}</td>
        <td>${tableDataArr[i].class}</td>
        <td>${tableDataArr[i].score}</td>`
        //将tr追加到tbody中
        document.querySelector('.tbody').appendChild(trObj)
    }
</script>
<script src="./page.js"></script>
<script>
    let page1 = new Page('.page1', {
        pageTag: {
            first: '首页',
            prev: '上一页',
            list: null,         //后期放页码
            next: '下一页',
            last: '末页'
        },
        pageInfo: {
            pageNum: 1,         //当前页数
            pageSize: 10,       //每页显示条数
            totalData: 1000,    //总条数
            totalPage: 100,     //总页数=向上取整(总条数/每页显示条数)
        },
        //callback回调函数 也就是你传递的函数，等需求时间到了直接调用
        //callback：function(){}
        //callback:()=>{}
        callback(pagenum) {
            console.log('回调函数当前页' + pagenum);
            let pagesize = 10
            let startnum = (pagenum - 1) * pagesize
            let html = ''
            for (let i = startnum; i < startnum + pagesize; i++) {
                //const element = array[i];
                html += `
                <tr>
                <td>${i + 1}</td>
                <td>${tableDataArr[i].id}</td>
                <td>${tableDataArr[i].name}</td>
                <td>${tableDataArr[i].age}</td>
                <td>${tableDataArr[i].gender}</td>
                <td>${tableDataArr[i].class}</td>
                <td>${tableDataArr[i].score}</td>
                </tr>`
            }
            document.querySelector('.tbody').innerHTML = html
        }
    })
</script>
</html>



























